#encoding: utf-8
#/usr/bin/env python

from sqlalchemy import Column,Integer,String,ForeignKey,Float,create_engine,or_ , not_,DateTime
from sqlalchemy.orm import sessionmaker,scoped_session,relationship
from sqlalchemy.ext.declarative import declarative_base

BaseModel = declarative_base()


class BLOCK(BaseModel):
    __tablename__ = 'block'
    id = Column(Integer, primary_key=True)
    blk_height = Column(Integer,nullable=False)
    blk_hash = Column(String(64), nullable=False)
    blk_size = Column(Integer, nullable=False)
    blk_version = Column(Integer, nullable=False)
    blk_merkleroot = Column(String(64))
    blk_time = Column(Integer, nullable=False)
    blk_nonce = Column(Integer, nullable=False)
    blk_bits = Column(Integer)
    blk_difficulty = Column(Float)

class TRANSACTION(BaseModel):
    __tablename__ = 'tradeinfo'
    id = Column(Integer, primary_key=True)
    tx_hash = Column(String(64),nullable=False)
    tx_version = Column(Integer)
    tx_locktime = Column(Integer)
    tx_time = Column(Integer)
    blk_height = Column(Integer, nullable=False)

class V_IN(BaseModel):
    __tablename__ = 'v_in'
    id = Column(Integer, primary_key=True)
    tx_hash = Column(String(64),nullable=False)
    intx_hash = Column(String(64))
    raw_msg = Column(String(1024))

class V_OUT(BaseModel):
    __tablename__ = 'v_out'
    id = Column(Integer, primary_key=True)
    tx_hash = Column(String(64),nullable=False)
    amount  = Column(Float,nullable=False)
    address = Column(String(40))
    raw_msg = Column(String(1024))


SQLITE_CONN_STR='sqlite:///./database'
#MYSQL_CONN_STR ='mysql+mysqldb://sql_user:j]zYtc-Elnk2@localhost/svr?charset=utf8'
#POSTGRES_CONN_STR='postgresql+psycopg2://litecoin:EeBpBAZK3bagOxg3@localhost:5432/ltc'
#engine = create_engine(MYSQL_CONN_STR,echo = True)
#engine = create_engine(SQLITE_CONN_STR,echo = True)
engine = create_engine(SQLITE_CONN_STR,echo = False)
#Base.metadata.drop_all(engine)
#Base.metadata.create_all(engine)
Session_Factory = sessionmaker(bind=engine)
Session = scoped_session(Session_Factory)


if __name__ == '__main__':
    BaseModel.metadata.create_all(engine)
    
